$(function(){

  $('a.edit').click(function() {
    $('#edit-mall-modal').modal();
    var that = $(this);

    $fub = $('#edit-mall-modal a.upload-logo');
 
    var uploader = new qq.FineUploaderBasic({
      button: $fub[0],
      request: {
        endpoint: '/mall/upload_logo',
        inputName: 'logo',
        forceMultipart: true
      },
      validation: {
        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'],
        sizeLimit: 1000 * 1024 // 200 kB = 200 * 1024 bytes
      },
      callbacks: {
        onValidate: function(fileData) {
          if (fileData[0].size > 1000 * 1024) {
            alert('图片太大了');
            return false;
          }
          var regImg = new RegExp(/\.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|jpeg|JPEG)$/);
          if(!fileData[0].name.match(regImg)) {
            alert('请上传gif,jpg,png格式文件');
            return false;
          }
        },
        onComplete: function(id, fileName, responseJSON) {
          if (responseJSON.success) {
            $('#add-mall-modal input[name=logo_url]').val(responseJSON.path);
            $('#add-mall-modal .snap').html('<img src='+responseJSON.path+' style="width:120px;"/>');
          } else {
            alert('上传失败!');
          }
        }
      }
    });

    $('#edit-mall-modal').on('hidden', function () {
      var data = {'cname':'', 'ename':'', 'id':'', 'description':'', 'type_id':'', 'logo_url':'', 'url':''};
      for (k in data) {
        $('#edit-mall-modal input[name='+k+']').val('');
      }
      $('#edit-mall-modal .msg').empty();
      $('#edit-mall-modal .snap').empty();
      $('#edit-mall-modal textarea[name=description]').val('');
    })
  
    $('#edit-mall-modal input').focus(function() {
      $('#edit-mall-modal .msg').empty();
    });


    var data = {'cname':'', 'ename':'', 'id':'', 'description':'', 'type_id':'', 'logo_url':'', 'url':''};
    for (k in data) {
      $('#edit-mall-modal input[name='+k+']').val(that.attr('data-'+k.replace('_', '-')));
    }
    $('#edit-mall-modal textarea[name=description]').val(that.attr('data-description'));
    $('#edit-mall-modal .snap').html('<img src="'+that.attr('data-logo-url')+'" style="width:120px;">');
    $('#edit-mall-modal select[name=type_id]').val(that.attr('data-type-id'));

    $('#edit-mall-modal a.save').unbind();
    $('#edit-mall-modal a.save').click(function() {
      var data = {'cname':'', 'ename':'', 'id':'', 'description':'', 'type_id':'', 'logo_url':'', 'url':''};
      for (k in data) {
        data[k] = $('#edit-mall-modal input[name='+k+']').val();
      }
      data['description'] = $('#edit-mall-modal textarea[name=description]').val();
      data['type_id'] = $('#edit-mall-modal select[name=type_id]').val();
    
      $.post('/mall/edit_mall/', data, function(ret) {
        if (ret.code == code.succ) {
          $('#edit-mall-modal').modal('hide');
          window.location.reload();
        } else {
          $('#edit-mall-modal div.msg').html(ret.msg);
        }
      }, 'json');
    });
  });

  $('a.delete').click(function() {
    if (confirm('确定要删除吗?')) {
    var that = $(this);
    var data = {'id':''};
    data.id = $(this).attr('data-id');
    $.post('/mall/delete_mall/', data, function(ret) {
      if (ret.code == code.succ) {
        that.parent().parent().fadeOut('slow');
      } else {
        alert('删除失败');
      }
    }, 'json');
    }
  });


  $('#add-mall').click(function() {
    $('#add-mall-modal').modal();

    $fub = $('#add-mall-modal a.upload-logo');
 
    var uploader = new qq.FineUploaderBasic({
      button: $fub[0],
      request: {
        endpoint: '/mall/upload_logo',
        inputName: 'logo',
        forceMultipart: true
      },
      validation: {
        allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'],
        sizeLimit: 1000 * 1024 // 200 kB = 200 * 1024 bytes
      },
      callbacks: {
        onValidate: function(fileData) {
          if (fileData[0].size > 1000 * 1024) {
            alert('图片太大了');
            return false;
          }
          var regImg = new RegExp(/\.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|jpeg|JPEG)$/);
          if(!fileData[0].name.match(regImg)) {
            alert('请上传gif,jpg,png格式文件');
            return false;
          }
        },
        onComplete: function(id, fileName, responseJSON) {
          if (responseJSON.success) {
            $('#add-mall-modal input[name=logo_url]').val(responseJSON.path);
            $('#add-mall-modal .snap').html('<img src='+responseJSON.path+' style="width:120px;"/>');
          } else {
            alert('上传失败!');
          }
        }
      }
    });


    $('#add-mall-modal').on('hidden', function () {
      var data = {'cname':'', 'ename':'', 'url':'', 'description':'', 'logo_url':'', 'type_id':''};
        for (k in data) {
        $('#add-mall-modal input[name='+k+']').val('');
      }
      $('#add-mall-modal .msg').empty();
      $('#add-mall-modal .snap').empty();
    })
  
    $('#add-mall-modal input').focus(function() {
      $('#add-mall-modal .msg').empty();
    });



    $('#add-mall-modal a.save').unbind();
    $('#add-mall-modal a.save').click(function() {
      var data = {'cname':'', 'ename':'', 'url':'', 'logo_url':'', 'description':'', 'type_id':''};
        for (k in data) {
        data[k] = $('#add-mall-modal input[name='+k+']').val();
      }
      data['description'] = $('#add-mall-modal textarea[name=description]').val();
      data['type_id'] = $('#add-mall-modal select[name=type_id]').val();
    
      $.post('/mall/new/', data, function(ret) {
        if (ret.code == code.succ) {
          $('#add-mall-modal').modal('hide');
          window.location.reload();
        } else {
          $('#add-mall-modal div.msg').html(ret.msg);
        }
      }, 'json');
    });

  });
});
